package com.bw.common;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.body.RequestBody;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class IDCardUtils {

    public static final String URL= "https://idenauthen.market.alicloudapi.com/idenAuthentication";
    // todo 改成自己 APP_CODE
    public static final String APP_CODE= "636bc62f6747422989726a2e39c4f372";

    /**
     * 实名认证
     * @param name
     * @param idNo
     * @return
     */
   public static Boolean checkIdCard(String name,String idNo){


       return postData(APP_CODE, URL, name, idNo);
    }


    public static Boolean postData(String appCode, String url, String name, String idNo){


        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("name", name);
        requestMap.put("idNo", idNo);
        HttpRequest httpRequest = HttpUtil.createPost(url).header("Authorization", "APPCODE " + appCode).form(requestMap);

        HttpResponse execute = null;
        try {
             execute = httpRequest.execute();
        } catch (Exception e) {
            System.out.println("++++++++++++调用实名认证api失败:" + e.getMessage());
        }

        if (execute != null){
            System.out.println(execute.body());
            JSONObject jsonObject = JSONUtil.parseObj(execute.body());
            Object respCode = jsonObject.get("respCode");
            if ("0000".equals(respCode)){
                return true;
            }else {
                Object respMessage = jsonObject.get("respMessage");
                System.out.println("+++++++++++实名查验结果：" + respMessage );
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Boolean b = checkIdCard("胡聪聪", "1234567890");
        System.out.println(b);
    }
}